package jp.co.redfox.archclub.midi;

import javax.sound.midi.*;

public class TestSequencer {
    public static void main(String[] args) throws Exception {

        try {
            System.out.println(javax.sound.midi.MidiSystem.getSequencer());
            System.exit(0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 演奏データの作成
        Sequence sequence = new Sequence(Sequence.PPQ, 96);
        Track track = sequence.createTrack();
        track.add(NoteOn(0, 60, 120));
        track.add(NoteOff(90, 60, 0));
        track.add(NoteOn(96, 62, 120));
        track.add(NoteOff(188, 62, 0));
        track.add(NoteOn(192, 64, 120));
        track.add(NoteOff(282, 64, 0));
        // 演奏
        Sequencer sequencer = MidiSystem.getSequencer();
        sequencer.open();
        sequencer.setSequence(sequence);
        sequencer.start();
        while (sequencer.isRunning())
            Thread.sleep(100);
        sequencer.close();
    }

    // イベントの作成
    public static MidiEvent NoteOn(int time, int noteno, int velocity) throws Exception {
        ShortMessage sm = new ShortMessage();
        sm.setMessage(ShortMessage.NOTE_ON, noteno, velocity);
        return new MidiEvent(sm, time);
    }

    public static MidiEvent NoteOff(int time, int noteno, int velocity) throws Exception {
        ShortMessage sm = new ShortMessage();
        sm.setMessage(ShortMessage.NOTE_OFF, noteno, velocity);
        return new MidiEvent(sm, time);
    }
}